let laypage = layui.laypage;
let form = layui.form

let data = {
  pagenum : 1,
  pagesize : 2,
  cate_id:'',
  state:''
}

// 页面渲染
function get_article() {
  $.ajax({
    url:'/my/article/list',
    data : data ,
    success: function(res) {
      // console.log(res)
      paging(res.total)
      let htmlStr = template('article_list',res)
      $('tbody').html(htmlStr)
    }
  })
  
}
get_article()

// 分页
function paging(total) {
  laypage.render({
    elem: 'demo7',
    curr: data.pagenum,
    count: total,
    limit: data.pagesize,
    limits:[2, 3, 5, 10],
    layout: ['count','limit', 'prev', 'page', 'next',  'skip'],
    jump: function(obj,first){
      // console.log(obj)
      if(!first) {
        data.pagenum = obj.curr,
        data.pagesize = obj.limit
        get_article()
      }
       
     
    }
  });
}

// 删除按钮
$('tbody').on('click','button:contains(删除)',function() {
  // console.log($(this).data('id'))
  let id = $(this).data('id')
  layer_ask = layer.confirm('你确定要删除吗？', {
    btn: ['确定','取消'] 
  }, function(){
    $.ajax({
      url:'/my/article/delete/' + id,
      success:function(res) {
        layer.msg(res.message)
        if(res.status === 0) {
          get_article()
        }
      }
    })
  }, function(){
    layer.close(layer_ask)
  });
})

// 下拉框
$.ajax({
  url:'/my/article/cates',
  success:function(res) {
    let htmlStr = template('all_categories',res)
    $('#all_categ').html(htmlStr);
    form.render('select');
  }
})

$('form').on('submit',function(e) {
  e.preventDefault()
  let cate_id = $('select[name=cate_id]').val()
  let state = $('select[name=state]').val()
  data.cate_id = cate_id
  data.state = state
  data.pagenum = 1
  get_article()
})